<?php

namespace app\admin\model;
use think\Model;
use think\Db;

class ArticleModel extends Model
{
    protected $name = 'article';
    
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    public function getByCount($map)
	{
		return Db::name('article')->where($map)->count();
	}

    /**
     * 根据搜索条件获取用户列表信息
     * @author 【朱沪宁】
     */
    public function getArticleByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_article.*,username as name')->join('think_admin', 'think_article.uid = think_admin.id')->where($map)->page($Nowpage, $limits)->order('think_article.create_time desc')->select();
    }
    public function getZanArticleByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_article.*,name')->join('think_article_cate', 'think_article.cate_id = think_article_cate.id')->where($map)->page($Nowpage, $limits)->order('think_article.views desc')->select();
    }
    
    public function getOrgByArticle()
    {
        $sql = "Select * from (SELECT org.title, (SELECT COUNT(a.id) FROM think_article a WHERE a.organization_id = org.id AND a.status=1) AS count2,";
        $sql .= "(SELECT COUNT(a.id) FROM think_article a WHERE a.organization_id = org.id) AS count1,";
        $sql .= "(SELECT COUNT(a.id) FROM think_article a WHERE a.organization_id = org.id AND a.status=1  AND a.is_tui=1) AS cncount,";
        $sql .= "(SELECT COUNT(a.id) FROM think_article a WHERE a.organization_id = org.id AND a.status=1  AND a.is_tui=0) AS nodafucount,";
        $sql .= "(SELECT COALESCE(SUM(a.tiyiren_sum),0) FROM think_article a WHERE a.organization_id = org.id ) AS tiyirencount FROM think_organization org) as tongji order by count2 desc";
        return $this->query($sql); 
    }

    public function getAscArticleByWhere($map, $Nowpage, $limits)
    {
        return $this->field('think_article.*,name')->join('think_article_cate', 'think_article.cate_id = think_article_cate.id')->where($map)->page($Nowpage, $limits)->order('think_article.create_time asc')->select();
    }
    
    /**
     * [insertArticle 添加信息]
     * @author 【朱沪宁】
     */
    public function insertArticle($param)
    {
        try{
            $result = $this->allowField(true)->save($param);
            if(false === $result){             
                return ['code' => -1, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '活动添加成功'];
            }
        }catch( PDOException $e){
            return ['code' => -2, 'data' => '', 'msg' => $e->getMessage()];
        }
    }



    /**
     * [updateArticle 编辑信息]
     * @author 【朱沪宁】
     */
    public function updateArticle($param)
    {
        try{
            $result = $this->allowField(true)->save($param, ['id' => $param['id']]);
            if(false === $result){          
                return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
            }else{
                return ['code' => 1, 'data' => '', 'msg' => '活动编辑成功'];
            }
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }



    /**
     * [getOneArticle 根据信息id获取一条信息]
     * @author 【朱沪宁】
     */
    public function getOneArticle($id)
    {
        return $this->where('id', $id)->find();
    }



    /**
     * [delArticle 删除信息]
     * @author 【朱沪宁】
     */
    public function delArticle($id)
    {
        try{
            $this->where('id', $id)->delete();
            return ['code' => 1, 'data' => '', 'msg' => '信息删除成功'];
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }

    /**
     * 根据用户UID  获取用户 资料列表
     *
     */
    public function memberArticle($uid){

        return $this->alias('a')->join('think_article_member m','a.id = m.article_id')->where('m.member_id',$uid)->order('m.createtime desc')->select();
    }

    public function getAllArticle()
    {
        return $this->field('think_article.*')->order('id asc')->select();
    }

}